package com.ruoyi.goods.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.api.RemoteWarehouseService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.goods.domain.BmGoods;
import com.ruoyi.goods.service.IBmGoodsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品信息管理Controller
 * 
 * @author ruoyi
 * @date 2023-08-02
 */
@RestController
@RequestMapping("/goods")
public class BmGoodsController extends BaseController
{
    @Autowired
    private IBmGoodsService bmGoodsService;
    @Autowired
    private RemoteWarehouseService remoteWarehouseService;

    /**
     * 查询商品信息管理列表
     */
    @RequiresPermissions("ruoyi-goods:goods:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody BmGoods bmGoods)
    {
        System.out.println("???"+bmGoods);
        startPage(bmGoods.getPageNum(),bmGoods.getPageSize());
        List<BmGoods> list = bmGoodsService.selectBmGoodsList(bmGoods);
//        System.out.println("list = " + list);
        return getDataTable(list);
    }

    /**
     * 导出商品信息管理列表
     */
    @RequiresPermissions("ruoyi-goods:goods:export")
    @Log(title = "商品信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmGoods bmGoods)
    {
        List<BmGoods> list = bmGoodsService.selectBmGoodsList(bmGoods);
        ExcelUtil<BmGoods> util = new ExcelUtil<BmGoods>(BmGoods.class);
        util.exportExcel(response, list, "商品信息管理数据");
    }

    /**
     * 获取商品信息管理详细信息
     */
    @RequiresPermissions("ruoyi-goods:goods:query")
    @GetMapping( "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bmGoodsService.selectBmGoodsById(id));
    }

    /**
     * 新增商品信息管理
     */
    @RequiresPermissions("ruoyi-goods:goods:add")
    @Log(title = "商品信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BmGoods bmGoods) throws IOException {
        return toAjax(bmGoodsService.insertBmGoods(bmGoods));
    }

    /**
     * 修改商品信息管理
     */
    @RequiresPermissions("ruoyi-goods:goods:edit")
    @Log(title = "商品信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmGoods bmGoods) throws IOException {
        return toAjax(bmGoodsService.updateBmGoods(bmGoods));
    }

    /**
     * 删除商品信息管理
     */
    @RequiresPermissions("ruoyi-goods:goods:remove")
    @Log(title = "商品信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bmGoodsService.deleteBmGoodsByIds(ids));
    }
    @DeleteMapping("/specification/{id}")
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(bmGoodsService.deleteBmGoodsBySpecificationId(id));
    }

//    /**
//     * 通过仓库编号查找仓库名称
//     */
//    @GetMapping("/{warehouseCode}")
//    public AjaxResult getInfo(@PathVariable("warehouseCode") String warehouseCode){
//        return AjaxResult.success(remoteWarehouseService.getInfo(warehouseCode));
//    }

    /**
     * 得到仓库名称列表
     */
    @GetMapping("/getWarehouseList")
    public AjaxResult getWarehouseList(){
        return AjaxResult.success(remoteWarehouseService.getWarehouseList());
    }
    @GetMapping("/getMaxGoodsCode")
    public String getMaxGoodsCode(){
        return bmGoodsService.getMaxGoodsCode();
    }
    @GetMapping("/getGoodsNumByWarehouseCode/{warehouseCode}")
    public Integer getGoodsNumByWarehouseCode(@PathVariable("warehouseCode") String warehouseCode) {
        Integer goodsNum = bmGoodsService.getGoodsNumByWarehouseCode(warehouseCode);
        return goodsNum;
    }

    @GetMapping("/addGoodsList")
    public AjaxResult addGoodsList(){
        List<Map<String, Object>> list = bmGoodsService.addGoodsList();
        return AjaxResult.success(list);
    }
}
